package edu.washington.cs.workflow.spec4j.example;

import java.awt.Button;
import java.util.Arrays;
import java.util.Collection;

import org.uispec4j.UISpec4J;
import org.uispec4j.UISpecTestCase;
import org.uispec4j.Window;
import org.uispec4j.interception.MainClassAdapter;

import edu.washington.cs.workflow.analysis.guistructure.FeedbackDirectedTester;
import edu.washington.cs.workflow.analysis.guistructure.Spec4JUtils;
import edu.washington.cs.workflow.analysis.guistructure.uievents.UIEvent;
import edu.washington.cs.workflow.analysis.guistructure.uievents.UIEvent.WINDOW;
import junit.framework.TestCase;

public class TestJEditProgram extends UISpecTestCase {

	static {
        UISpec4J.init();
    } 
	
	protected void setUp() throws Exception {
//		Class<?> mainClass = Class.forName("org.homeunix.thecave.buddi.Buddi");
//		Class<?> mainClass = Class.forName("com.act365.sudoku.SuDoku");
//	    Class<?> mainClass = Class.forName("org.gjt.sp.jedit.jEdit");
		Class<?> clz = Class.forName("org.gjt.sp.jedit.jEdit");
	    setAdapter(new MainClassAdapter(clz, new String[0]));
	}
	
	public void testExecuteEvents() {
		System.out.println("getting window");
		Window window = getMainWindow();
		System.err.println("window: " + window);
		System.err.println(window);
		System.err.println(window.getDescription());
//		org.homeunix.thecave.buddi.Buddi.main(new String[0]);
//		com.act365.sudoku.SuDoku.main(new String[]{});
		
		//it shows 2 windows in a line
		Collection<UIEvent> events = Spec4JUtils.allNonNullRandomUIEvents(window);
		
		for(UIEvent event : events) {
			System.err.println(event);
//			if(event.toString().indexOf("Go to Previous Buffer") == -1
//					&& event.toString().indexOf("Go to Next Buffer") == -1) {
//				continue;
//			}
			try {
			    WINDOW w = event.probeWindowInfo();
			    event.fireEvent();
			} catch (Error e) {
				System.err.println(e);
			}
//			break;
		}
	}
	
	public void testFeedbackDirected() {
//		System.setOut(out);
		FeedbackDirectedTester.testNum = 50;
		FeedbackDirectedTester.printMap = true;
		FeedbackDirectedTester.popupWindowTime = 1000;
		FeedbackDirectedTester.main(new String[]{"org.gjt.sp.jedit.jEdit"});
	}
	
//	public static void main(String[] args) {
////		com.act365.sudoku.SuDoku.main(new String[]{});
//		//org.gjt.sp.jedit.jEdit.main(new String[]{});
//		
//		TestGUIExampleProgram program = new TestGUIExampleProgram();
//		program.setAdapter(new MainClassAdapter(org.gjt.sp.jedit.jEdit.class, new String[0]));
//		System.out.println("getting window");
//		Window window = program.getMainWindow();
//		System.out.println("window: " + window);
//	}
}
